package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Timer;

@Component
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 处理支付超时的订单
     */
    @Scheduled(cron = "0 * * * * ?")
    public void processPayTimeOut(){
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Orders> ordersList = orderMapper.getTimeOutOrders(Orders.PENDING_PAYMENT,time);

        for (Orders orders : ordersList){
            orders.setCancelReason("支付超时");
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.upDate(orders);
        }
    }

    /**
     * 处理送达未完成订单
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processCompleteTimeOut(){
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
        List<Orders> ordersList = orderMapper.getTimeOutOrders(Orders.DELIVERY_IN_PROGRESS,time);

        for (Orders orders : ordersList){
            orders.setStatus(Orders.COMPLETED);
            orders.setDeliveryTime(LocalDateTime.now());
            orderMapper.upDate(orders);
        }
    }
}
